﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Windows.Controls;

namespace WebSocketAndPrint
{
    public class ViewData : PropertyStore
    {
        #region 单例
        private static ViewData _ViewData = new ViewData();
        private ViewData() { }

        public static ViewData GetInstance()
        {
            return _ViewData;
        }
        #endregion


        #region WebSocket 显示对象
        /// <summary>
        /// 加入需要显示的数据
        /// </summary>
        public void PushSocket(WebSocket webSocket)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                SynchronizationContext.SetSynchronizationContext(new System.Windows.Threading.DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                SynchronizationContext.Current.Post(pl =>
                {//里面写真正的业务内容
                    this._SocketList.Add(webSocket);//实体类里属性一定要有get方法，否则会显示  BindingExpression path error: 'B' property not found on 'object' ''TT' 错误
                }, null);
            });
            SocketList = _SocketList;
        }

        /// <summary>
        /// 删除不要的对象
        /// </summary>
        public void PopSocket(long socketHashCode)
        {
            WebSocket webSocket = this._SocketList.Where(x => x.HashCode == socketHashCode).Single();
            if (webSocket != null)
            {
                ThreadPool.QueueUserWorkItem(delegate
                {
                    SynchronizationContext.SetSynchronizationContext(new System.Windows.Threading.DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                    SynchronizationContext.Current.Post(pl =>
                    {//里面写真正的业务内容
                        this._SocketList.Remove(webSocket);
                    }, null);
                });
            }
        }

        /// <summary>
        /// 获取昵称
        /// </summary>
        /// <param name="socketHashCode"></param>
        /// <returns></returns>
        public String GetNickName(long socketHashCode)
        {
            return this._SocketList.Where(x => x.HashCode == socketHashCode).Single().NickName;
        }

        /// <summary>
        /// 修改昵称
        /// </summary>
        /// <param name="socketHashCode"></param>
        /// <param name="viewName"></param>
        public void ReNickName(long socketHashCode,String viewName)
        {
            WebSocket webSocket = this._SocketList.Where(x => x.HashCode == socketHashCode).Single();
            ThreadPool.QueueUserWorkItem(delegate
            {
                SynchronizationContext.SetSynchronizationContext(new System.Windows.Threading.DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                SynchronizationContext.Current.Post(pl =>
                {//里面写真正的业务内容
                    webSocket.NickName = viewName;//这个属性也需要实现 NotifyPropertyChanged 属性
                }, null);
            });
        }

        /// <summary>
        /// 实际保存的数据容器
        /// </summary>
        private ObservableCollection<WebSocket> _SocketList = new ObservableCollection<WebSocket>();

        /// <summary>
        /// WebSocket 显示列表
        /// </summary>
        public ObservableCollection<WebSocket> SocketList
        {
            get { return _SocketList; }
            private set => Set(value);//如果不私有化,在此类外可以直接赋值,那么对应的指针就变了,就不会自动更新UI了
        }

        /// <summary>
        /// 当前选择的 WebSocket对象
        /// </summary>
        public WebSocket SelectSocket
        {
            get => Get<WebSocket>();
            set => Set(value);
        }
        #endregion


        #region 本地IP地址
        public String IP
        {
            get => Get<String>();
            set => Set(value);
        }
        public String Port
        {
            get => Get<string>();
            set => Set(value);
        }
        #endregion

        #region 消息列表

        public RichTextBox RichTextBoxObj = null;

        public void PushTextBox(String text)
        {
            if (RichTextBoxObj != null)
            {
                ThreadPool.QueueUserWorkItem(delegate
                {
                    SynchronizationContext.SetSynchronizationContext(new System.Windows.Threading.DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                    SynchronizationContext.Current.Post(pl =>
                    {//里面写真正的业务内容                        
                        RichTextBoxObj.AppendText(text + "\r\n");
                    }, null);
                });
                
            }
        }
        #endregion

    }

}
